/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is
*   Nick Kreeger
* Portions created by the Initial Developer are Copyright (C) 2006-2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   Nick Kreeger <nick.kreeger@park.edu>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

#import "MessageHeader.h"
#import "NSDate+Utils.h"
#import "NSString+Utils.h"

#import "nsString.h"

@implementation MessageHeader

-(id)init
{
  if ((self = [super init]))
  {
    mRepMsgHdr = nsnull;
  }
  
  return self;
}

-(id)initWithMsgDBHdr:(nsIMsgDBHdr*)aMsgDBHdr
{
  if ((self = [super init]))
  {
    NS_IF_ADDREF(aMsgDBHdr);
    mRepMsgHdr = aMsgDBHdr;
  }
  
  return self;
}

-(void)dealloc
{
  NS_IF_RELEASE(mRepMsgHdr);
  [super dealloc];
}

-(BOOL)isFlagged
{
  PRBool isFlagged = NO;
  if (mRepMsgHdr)
    mRepMsgHdr->GetIsFlagged(&isFlagged);
  
  return isFlagged;
}

-(BOOL)isRead
{
  PRBool isRead = NO;
  if (mRepMsgHdr)
    mRepMsgHdr->GetIsRead(&isRead);
  
  return isRead;
}

-(NSString*)author
{
  nsresult rv;
  nsXPIDLCString msgAuthor;
  rv = mRepMsgHdr->GetAuthor(getter_Copies(msgAuthor));

  // This might not be fast enough, if this causes problems (since it has to run through libmime)
  // let's try sniffing out the char-encoding on a 8-bit string.
  nsXPIDLString authorStr;
  rv = mRepMsgHdr->GetMime2DecodedAuthor(getter_Copies(authorStr));
  if (NS_FAILED(rv))
    return @"Could not get author";  // ugly.
  
  NSString* author = [NSString stringWith_nsAString:authorStr];

  // Remove the <email_addr>
  NSRange removeRange = [author rangeOfString:@" <"];
  if (removeRange.location != NSNotFound && removeRange.location > 0)
  {
    NSRange strRange = NSMakeRange(0, removeRange.location);
    
    // If this string has "" remove them.
    if ([author characterAtIndex:0] == '\"')
    {
      strRange.location++;
      strRange.length = strRange.length - 2;
    }
    
    author = [author substringWithRange:strRange];
  }
    
  return author;
}

-(NSString*)subject
{
  // Same thing with the above, watch out for encoding issues.
  nsXPIDLString subject;
  nsresult rv = mRepMsgHdr->GetMime2DecodedSubject(getter_Copies(subject));
  if (NS_FAILED(rv))
    return @"";
  
  return [[NSString stringWith_nsAString:subject] stringByTrimmingWhitespaceAndNewlines];
}

-(NSDate*)date
{
  nsresult rv;
  PRTime date;
  
  rv = mRepMsgHdr->GetDate(&date);
  if (NS_FAILED(rv))
    return [[NSDate alloc] init];
  
  return [NSDate dateWithPRTime:date];
}

-(NSString*)messageId
{
  nsresult rv;
  NSString* messageId = nil;
  
  nsXPIDLCString msgId;
  rv = mRepMsgHdr->GetMessageId(getter_Copies(msgId));
  if (NS_SUCCEEDED(rv))
    messageId = [NSString stringWithUTF8String:msgId];
  
  return messageId;
}

-(nsIMsgDBHdr*)representedMsgHdr
{
  return mRepMsgHdr;
}

-(NSComparisonResult)compareIsRead:(MessageHeader*)aMessageHeader
{
  if (([self isRead] && [aMessageHeader isRead]) || 
      (![self isRead] && ![aMessageHeader isRead])) 
  {
    return NSOrderedSame;
  }
  else if ([self isRead] && ![aMessageHeader isRead])
    return NSOrderedAscending;
  else
    return NSOrderedDescending;
}

-(NSComparisonResult)compareIsFlagged:(MessageHeader*)aMessageHeader
{
  if (([self isFlagged] && [aMessageHeader isFlagged]) || 
      (![self isFlagged] && ![aMessageHeader isFlagged])) 
  {
    return NSOrderedSame;
  }
  else if ([self isFlagged] && ![aMessageHeader isFlagged])
    return NSOrderedAscending;
  else
    return NSOrderedDescending;
}

-(NSComparisonResult)compareAuthor:(MessageHeader*)aMessageHeader
{
  return [[self author] compare:[aMessageHeader author]];
}

-(NSComparisonResult)compareSubject:(MessageHeader*)aMessageHeader
{
  return [[self subject] compare:[aMessageHeader subject]];
}

-(NSComparisonResult)compareDate:(MessageHeader*)aMessageHeader
{
  return [[self date] compare:[aMessageHeader date]];
}

@end
